package com.sdgakj.quartz;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sdgakj.entity.Person;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
 * Created by win10 on 2017/6/5.
 */
public class ChartGraphics {
    private BufferedImage image;
    private int imageWidth = 1200;  //图片的宽度
    private int imageHeight = 1000; //图片的高度
    //生成图片文件
    @SuppressWarnings("restriction")
    public void createImage(String fileLocation) {
        BufferedOutputStream bos = null;
        if(image != null){
            try {
                FileOutputStream fos = new FileOutputStream(fileLocation);
                bos = new BufferedOutputStream(fos);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
                encoder.encode(image);
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                if(bos!=null){//关闭输出流
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    public void graphicsGeneration(List<Person> list,int index) {
        image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        //设置图片的背景色
        Graphics2D main = image.createGraphics();
        main.setColor(Color.white);
        main.fillRect(0, 0, imageWidth, imageHeight);

        Graphics2D border = image.createGraphics();
        border.setColor(Color.black);
        //内边框 上 左 右 下1
        border.fillRect(0, 0, 1200, 5);//左 上 宽 高
        border.fillRect(0, 0, 5, 1000);//左边框
        border.fillRect(1195, 0, 5, 1000);//右边框
        border.fillRect(0,995,1200,5);//下边框
        //内边框 上 左 右 下2
        border.fillRect(8, 8, 1180, 4);
        border.fillRect(8, 8, 4, 980);
        border.fillRect(1187, 8, 4, 980);
        border.fillRect(8, 988, 1183, 4);
        //中间 分页
        border.fillRect(550, 8, 1, 980);
        border.fillRect(650, 8, 1, 980);
        border.fillRect(550, 280, 100, 10);
        /**
         * 左侧 行
         */
        border.setColor(Color.RED);
        //显示公元
        border.fillRect(10, 30, 540, 2);
        //行分割线
        border.setColor(Color.RED);
        border.fillRect(10, 220, 540, 2);
        border.fillRect(10, 420, 540, 2);
        border.fillRect(10, 620, 540, 2);
        border.fillRect(10, 820, 540, 2);
        /**
         * 右侧 行
         */
        border.fillRect(650, 30, 535, 2);

        //行分割线
        border.setColor(Color.RED);
        border.fillRect(650, 220, 535, 2);
        border.fillRect(650, 420, 535, 2);
        border.fillRect(650, 620, 535, 2);
        border.fillRect(650, 820, 535, 2);
        /**
         * 族谱头部
         */
        border.setColor(Color.BLACK);
        border.setFont(new Font("楷体", Font.BOLD, 60));
        int num=80;
        border.drawString("刘",570,num);
        num=num+60;
        border.drawString("氏",570,num);
        num=num+60;
        border.drawString("族",570,num);
        num=num+60;
        border.drawString("谱",570,num);
        //底部注释
        border.setFont(new Font("楷体",Font.BOLD,30));
        border.drawString(index+1+"", 560,960);
        border.drawString(index+"", 610, 960);
        //公元
       /* border.setColor(Color.BLACK);
        border.setFont(new Font("楷书", Font.BOLD, 20));
        border.drawString("公", 1000,28 );
        border.drawString("一", 1020,28 );
        border.drawString("公", 800,28 );
        border.drawString("二", 820,28 );
        border.drawString("公", 400,28 );
        border.drawString("三", 420,28 );
        border.drawString("公", 200,28 );
        border.drawString("四", 220,28 );*/
        //第几世
        /*border.setFont(new Font("楷书", Font.BOLD, 30));
        num=80;
        border.drawString("第",1100,num);
        num+=50;
        border.drawString("一",1100,num);
        num+=50;
        border.drawString("世",1100,num);*/
        /*int x=1050; //默认第一行起始位置  左边距
        int y=80;//默认第一行 的其实位置  上边距
        int n=0;//判断写到第几个人了
        int rows=1;//判断写到第几行*/
        List<String> name=new ArrayList<String>();//姓名信息
        List<String> address=new ArrayList<String>();//地址信息
        for(Person li:list){
            name.add(li.getName());
            address.add(li.getBornAddress());

        }
        addContext(1050,80,0,1,name,border,new Font("楷体",Font.BOLD,20));
        addContext(1030,80,0,1,address,border,new Font("楷体",Font.BOLD,18));
        createImage("D:\\"+list.get(0).getName().toCharArray()[0]+index+"式家族.jpg");
    }
    //解析参数
    public Graphics2D addContext(int x,int y,int n,int rows,List<String> lis,Graphics2D border,Font font){
        final int x1=x;
        Graphics2D context=border;
        for(String li:lis){
            char[] cname=li.toCharArray();
            for(char car:cname){
                context.setFont(font);
                context.drawString(String.valueOf(car), x, y);
                y+=30;
            }
            n=n+1;
            if(rows==1){
                if(n<4){
                    if(n==0 || n==1){
                        x-=200;
                    }else if(n==2){
                        x-=400;
                    }else{
                        x-=200;
                    }
                    y=80;
                }else{
                    x=x1+200;
                    y=280;
                    rows=rows+1;
                    n=0;
                }
            }
            if(rows==2){
                if(n<4){
                    if(n==0 || n==1){
                        x-=200;
                    }else if(n==2){
                        x-=400;
                    }else{
                        x-=200;
                    }
                    y=280;
                }else{
                    x=x1+200;
                    y=450;
                    rows++;
                    n=0;
                }
            }
            if(rows==3){
                if(n<4){
                    if(n==0 || n==1){
                        x-=200;
                    }else if(n==2){
                        x-=400;
                    }else{
                        x-=200;
                    }
                    y=480;
                }else{
                    x=x1+200;
                    y=680;
                    rows++;
                    n=0;
                }
            }
            if(rows==4){
                if(n<4){
                    if(n==0 || n==1){
                        x-=200;
                    }else if(n==2){
                        x-=400;
                    }else{
                        x-=200;
                    }
                    y=680;
                }else{
                    x=x1+200;
                    y=80;
                    rows++;
                    n=0;
                }
            }
            if(rows==5){
                if(n<4){
                    if(n==0 || n==1){
                        x-=200;
                    }else if(n==2){
                        x-=400;
                    }else{
                        x-=200;
                    }
                    y=880;
                }else{
                    x=x1+200;
                    y=80;
                    rows++;
                    n=0;
                    rows=0;
                }
            }
        }
        return context;
    }

}
